# -*- shell-script -*-

set -eu
[ x"${GMB_SHELL_DEBUG:-no}" = x"no" ] || set -x


# gmb_list_target_builds
#
gmb_list_target_builds()
{
    local target="${1}"
    local confdir="${GMB_BUILDS_CONFIG_DIR}/${target}"

    [ -d "${confdir}" ] || gmb_error "gmb_list_target_builds: config dir does not exist: '${confdir}'"

    find "${confdir}" -mindepth 1 -maxdepth 1 -type f -name '*.conf' -exec basename --suffix='.conf' {} \;
}


# gmb_config_global
#
gmb_config_global()
{
    # env_push/pop ignores all variables set before gmb run
    export GMB_ENV_STACK_IGNORE=$(env --null \
                                      | sed -z -e 's,=.*$,,g' \
                                      | sort -z \
                                      | tr '\0' ' ' \
                                      | gmb_strstrip)
    
    # system paths
    export GMB_PREFIX='@prefix@'
    export GMB_LIBEXECDIR="${GMB_PREFIX}/libexec/gmb"
    export GMB_CONFIG_DIR="${GMB_PREFIX}/etc/gmb"
    export GMB_VAR_DIR="${GMB_PREFIX}/var/gmb"
    export GMB_RUN_DIR="${GMB_VAR_DIR}/run"

    export GMB_DRIVERS_DIR="${GMB_LIBEXECDIR}/drivers"
    export GMB_SOURCES_CONFIG_DIR="${GMB_CONFIG_DIR}/sources.d"
    export GMB_REPOSITORIES_CONFIG_DIR="${GMB_CONFIG_DIR}/repositories.d"
    export GMB_PACKAGES_CONFIG_DIR="${GMB_CONFIG_DIR}/packages.d"
    export GMB_TARGETS_CONFIG_DIR="${GMB_CONFIG_DIR}/targets.d"
    export GMB_BUILDS_CONFIG_DIR="${GMB_CONFIG_DIR}/builds.d"
    export GMB_LOG_DIR="${GMB_VAR_DIR}/log"
    export GMB_STAMPS_DIR="${GMB_VAR_DIR}/stamps"

    export GMB_LOGFILE=
    
    export GMB_ENV_STACK_DIR="${GMB_RUN_DIR}/stack"
    export GMB_ENV_STACK_HEAD="${GMB_ENV_STACK_DIR}/head"
    export GMB_ENV_STACK_TAIL="${GMB_ENV_STACK_DIR}/tail"

    export GMB_CONFIG_FILE="${GMB_CONFIG_DIR}/gmb.conf"

    # [fixme] srcdir should be */usr/share or something
    export GMB_TOP_SOURCE_DIR="${HOME}/src"
    export GMB_TOP_BUILD_DIR="${GMB_VAR_DIR}/build"
    export GMB_TOP_PREFIX="${HOME}/gmb-local"
    
    export GMB_DEFAULT_SOURCE='default'
    export GMB_DEFAULT_TARGET='default'
    
    # runtime options
    export GMB_VERB_LEVEL=2
    export GMB_CMD_VERB_LEVEL=1
    export GMB_FORCE=0
    export GMB_FORCE_ALL=0
    export GMB_N_JOBS=4 # [FIXME] get n cpus
    
    # read system config
    gmb_debug "reading global config: '${GMB_CONFIG_FILE}'"
    if [ -f "${GMB_CONFIG_FILE}" ]; then
        . "${GMB_CONFIG_FILE}"
    fi

    : ${GMB_TOP_BUILD_DIR:=${GMB_VAR_DIR}/build}
    : ${GMB_TOP_PREFIX:=${HOME}/gmb-local}
    
    : ${GMB_DEFAULT_SOURCE:=default}
    : ${GMB_DEFAULT_TARGET:=default}
}


# gmb_config_target
#
gmb_config_target()
{
    export GMB_TARGET_NAME="${1}"
    local conf="${GMB_TARGETS_CONFIG_DIR}/${GMB_TARGET_NAME}.conf"

    gmb_debug "configuring target '${GMB_TARGET_NAME}' from '${conf}'"
    
    export GMB_TARGET_BUILD_DIR=
    export GMB_TARGET_PREFIX=

    [ -f "${conf}" ] || gmb_error "unknwon target: '${GMB_TARGET_NAME}' (${conf})"
    . "${conf}"

    : ${GMB_TARGET_BUILD_DIR:=${GMB_TOP_BUILD_DIR}/${GMB_TARGET_NAME}}
    : ${GMB_TARGET_PREFIX:=${GMB_TOP_PREFIX}/${GMB_TARGET_NAME}}
}


# gmb_config_build
#
gmb_config_build()
{
    export GMB_BUILD_NAME="${1}"
    local conf="${GMB_BUILDS_CONFIG_DIR}/${GMB_TARGET_NAME}/${GMB_BUILD_NAME}.conf"

    gmb_debug "configuring build '${GMB_BUILD_NAME}@${GMB_TARGET_NAME}' from '${conf}'"

    export GMB_BUILD_REPOSITORY=
    export GMB_BUILD_SOURCE_DIR=
    export GMB_BUILD_BUILD_DIR=
    export GMB_BUILD_PREFIX=
    export GMB_BUILD_IN_PLACE=
    export GMB_BUILD_DRIVER_CONFIGURE=
    export GMB_BUILD_DRIVER_BUILD=
    export GMB_BUILD_DRIVER_INSTALL=
    
    [ -f "${conf}" ] || gmb_error "unknown build: ${GMB_BUILD_NAME}@${GMB_TARGET_NAME} (${conf})"
    . "${conf}"

    : ${GMB_BUILD_REPOSITORY:=${GMB_BUILD_NAME}}
    : ${GMB_BUILD_BUILD_DIR:=${GMB_TARGET_BUILD_DIR}/${GMB_BUILD_NAME}}
    : ${GMB_BUILD_PREFIX:=${GMB_TARGET_PREFIX}}
    : ${GMB_BUILD_IN_PLACE:=no}

    gmb_config_repository "${GMB_BUILD_REPOSITORY}"

    if [ x"${GMB_BUILD_IN_PLACE}" = x"no" ]; then
        : ${GMB_BUILD_SOURCE_DIR:=${GMB_REPOSITORY_SOURCE_DIR}}
    else
        : ${GMB_BUILD_SOURCE_DIR:=${GMB_BUILD_BUILD_DIR}}
    fi
    
    # drivers
    : ${GMB_BUILD_DRIVER_CONFIGURE:=${GMB_PACKAGE_DRIVER_CONFIGURE}}
    : ${GMB_BUILD_DRIVER_BUILD:=${GMB_PACKAGE_DRIVER_BUILD}}
    : ${GMB_BUILD_DRIVER_INSTALL:=${GMB_PACKAGE_DRIVER_INSTALL}}

    gmb_config_driver "${GMB_BUILD_DRIVER_CONFIGURE}"
    gmb_config_driver "${GMB_BUILD_DRIVER_BUILD}"
    gmb_config_driver "${GMB_BUILD_DRIVER_INSTALL}"

    gmb_debug "build '${GMB_BUILD_NAME}@${GMB_TARGET_NAME}':"
    gmb_debug "  package    = '${GMB_PACKAGE_NAME}'"
    gmb_debug "  source dir = '${GMB_BUILD_SOURCE_DIR}' (from '${GMB_REPOSITORY_SOURCE_DIR}')"
    gmb_debug "  build dir  = '${GMB_BUILD_BUILD_DIR}'"
    gmb_debug "  prefix     = '${GMB_BUILD_PREFIX}'"
}


# gmb_config_repository
#
gmb_config_repository()
{
    export GMB_REPOSITORY_NAME="${1}"
    local conf="${GMB_REPOSITORIES_CONFIG_DIR}/${GMB_REPOSITORY_NAME}.conf"

    gmb_debug "configuring repository '${GMB_REPOSITORY_NAME}' from '${conf}'"

    export GMB_REPOSITORY_PACKAGE=
    export GMB_REPOSITORY_SOURCE=
    export GMB_REPOSITORY_SOURCE_DIR=
    export GMB_REPOSITORY_UPDATE=
    export GMB_REPOSITORY_UPDATE_DELAY=
    export GMB_REPOSITORY_DRIVER_CHECKOUT=
    export GMB_REPOSITORY_DRIVER_UPDATE=
    export GMB_REPOSITORY_DRIVER_BOOTSTRAP=
    
    [ -f "${conf}" ] || gmb_error "unknown repository: ${GMB_REPOSITORY_NAME} (${conf})"
    . "${conf}"

    : ${GMB_REPOSITORY_PACKAGE:=${GMB_REPOSITORY_NAME}}
    gmb_config_package "${GMB_REPOSITORY_PACKAGE}"
    
    : ${GMB_REPOSITORY_SOURCE:=${GMB_DEFAULT_SOURCE}}
    gmb_config_source "${GMB_REPOSITORY_SOURCE}"

    : ${GMB_REPOSITORY_SOURCE_DIR:=${GMB_SOURCE_SOURCE_DIR}/${GMB_REPOSITORY_NAME}}
    : ${GMB_REPOSITORY_UPDATE:=no}
    : ${GMB_REPOSITORY_UPDATE_DELAY:=$((24*3600))}
    
    # drivers
    : ${GMB_REPOSITORY_DRIVER_CHECKOUT:=${GMB_PACKAGE_DRIVER_CHECKOUT}}
    gmb_config_driver "${GMB_REPOSITORY_DRIVER_CHECKOUT}"

    : ${GMB_REPOSITORY_DRIVER_UPDATE:=${GMB_PACKAGE_DRIVER_UPDATE}}
    gmb_config_driver "${GMB_REPOSITORY_DRIVER_UPDATE}"

    : ${GMB_REPOSITORY_DRIVER_BOOTSTRAP:=${GMB_PACKAGE_DRIVER_BOOTSTRAP}}
    gmb_config_driver "${GMB_REPOSITORY_DRIVER_BOOTSTRAP}"
}


# gmb_config_package
#
gmb_config_package()
{
    export GMB_PACKAGE_NAME="${1}"
    local conf="${GMB_PACKAGES_CONFIG_DIR}/${GMB_PACKAGE_NAME}.conf"

    gmb_debug "configuring package '${GMB_PACKAGE_NAME}' from '${conf}'"

    export GMB_PACKAGE_URL=
    export GMB_PACKAGE_DRIVER=
    export GMB_PACKAGE_VC_DRIVER=
    export GMB_PACKAGE_DRIVER_CHECKOUT=
    export GMB_PACKAGE_DRIVER_UPDATE=
    export GMB_PACKAGE_DRIVER_BOOTSTRAP=
    export GMB_PACKAGE_DRIVER_CONFIGURE=
    export GMB_PACKAGE_DRIVER_BUILD=
    export GMB_PACKAGE_DRIVER_INSTALL=
    export GMB_PACKAGE_DEPENDS=
    
    [ -f "${conf}" ] || gmb_error "unknown package: ${GMB_PACKAGE_NAME}"
    . "${conf}"

    : ${GMB_PACKAGE_DRIVER:=nodriver}
    : ${GMB_PACKAGE_VC_DRIVER:=git}
    : ${GMB_PACKAGE_DRIVER_CHECKOUT:=${GMB_PACKAGE_VC_DRIVER}/checkout}
    : ${GMB_PACKAGE_DRIVER_UPDATE:=${GMB_PACKAGE_VC_DRIVER}/update}
    : ${GMB_PACKAGE_DRIVER_BOOTSTRAP:=${GMB_PACKAGE_DRIVER}/bootstrap}
    : ${GMB_PACKAGE_DRIVER_CONFIGURE:=${GMB_PACKAGE_DRIVER}/configure}
    : ${GMB_PACKAGE_DRIVER_BUILD:=${GMB_PACKAGE_DRIVER}/build}
    : ${GMB_PACKAGE_DRIVER_INSTALL:=${GMB_PACKAGE_DRIVER}/install}
    : ${GMB_PACKAGE_DEPENDS:=}
}



# gmb_config_source
#
gmb_config_source()
{
    export GMB_SOURCE_NAME="${1}"
    local conf="${GMB_SOURCES_CONFIG_DIR}/${GMB_SOURCE_NAME}.conf"

    gmb_debug "configuring source '${GMB_SOURCE_NAME}' from '${conf}'"

    export GMB_SOURCE_SOURCE_DIR=
    
    [ -f "${conf}" ] || gmb_error "unknown source: ${GMB_SOURCE_NAME}"
    . "${conf}"

    : ${GMB_SOURCE_SOURCE_DIR:=${GMB_TOP_SOURCE_DIR}/${GMB_SOURCE_NAME}}
}


# gmb_config_driver
#
gmb_config_driver()
{
    local driver="${1}"
    local conf="${GMB_DRIVERS_DIR}/${driver}.conf"

    gmb_debug "configuring driver '${driver}' from '${conf}'"

    [ -f "${conf}" ] || gmb_error "unknown driver: '${driver}' (${conf} not found)"
    . "${conf}"
}
